package com.ywd.materials.admin.controller;

import com.ywd.materials.admin.common.Common;
import com.ywd.materials.admin.context.UserContext;
import com.ywd.materials.admin.dto.AdminUserDto;
import com.ywd.materials.admin.dto.AdminUserLoginDto;
import com.ywd.materials.admin.dto.AdminUserUpdateDto;
import com.ywd.materials.admin.dto.query.UserPageQuery;
import com.ywd.materials.admin.exception.R;
import com.ywd.materials.admin.exception.RPage;
import com.ywd.materials.admin.service.impl.AdminRoleServiceImpl;
import com.ywd.materials.admin.service.impl.AdminUserServiceImpl;
import com.ywd.materials.admin.vo.AdminUserDetailsVo;
import com.ywd.materials.admin.vo.AdminUserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lk
 * @date 2024/4/17 14:02
 */
@AllArgsConstructor
@RestController
@RequestMapping("/adminUser")
@Tag(name = "用户管理")
public class AdminUserController {

    private final AdminUserServiceImpl adminUserServiceImpl;
    private final AdminRoleServiceImpl adminRoleServiceImpl;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public R<String> login(@RequestBody AdminUserLoginDto dto) {
        return R.ok(adminUserServiceImpl.login(dto));
    }

    /**
     * 退出登录
     */
    @GetMapping("/out")
    @Operation(summary = "退出登录")
    public R<Boolean> out() {
        return R.ok(adminUserServiceImpl.out());
    }

    /**
     * 用户修改密码
     */
    @PutMapping("/updatePassword")
    @Operation(summary = "用户修改密码")
    public R<Boolean> updatePassword(@RequestBody AdminUserUpdateDto dto) {
        return R.ok(adminUserServiceImpl.updatePassword(dto));
    }

    /**
     * 查询当前用户信息
     */
    @GetMapping("/getUserInfo")
    @Operation(summary = "查询当前用户信息")
    public R<AdminUserDetailsVo> getUserInfo() {
        return R.ok(adminUserServiceImpl.getUserInfo());
    }

    /**
     * 查询用户列表
     */
    @PostMapping("/getUserPage")
    @Operation(summary = "查询用户列表")
    public R<RPage<List<AdminUserVo>>> getUserPage(@RequestBody UserPageQuery query) {
        return R.ok(adminUserServiceImpl.getUserPage(query));
    }

    /**
     * 查询用户详情
     */
    @GetMapping("/getUserDetails")
    @Operation(summary = "查询用户详情")
    public R<AdminUserVo> getUserDetails(@Schema(description = "用户ID") @RequestParam("id") Long id) {
        return R.ok(adminUserServiceImpl.getUserDetails(id));
    }

    /**
     * 添加用户
     */
    @PostMapping("/saveUser")
    @Operation(summary = "添加用户")
    public R<Boolean> saveUser(@RequestBody AdminUserDto dto) {
        return R.ok(adminUserServiceImpl.saveUser(dto));
    }

    /**
     * 修改用户
     */
    @PutMapping("/updateUser")
    @Operation(summary = "修改用户")
    public R<Boolean> updateUser(@RequestBody AdminUserDto dto) {
        return R.ok(adminUserServiceImpl.updateUser(dto));
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/deleteUser")
    @Operation(summary = "删除用户")
    public R<Boolean> deleteUser(@Schema(description = "用户ID") @RequestParam("id") Long id) {
        return R.ok(adminUserServiceImpl.deleteUser(id));
    }

//    /**
//     * 用户列表(筛选项)
//     */
//    @GetMapping("/getUserPartList")
//    @Operation(summary = "用户列表(筛选项)")
//    public R<List<Common>> getUserPartList() {
//        return R.ok(adminUserServiceImpl.getUserPartList());
//    }

    /**
     * 角色列表(筛选项)
     */
    @GetMapping("/getRolePartList")
    @Operation(summary = "角色列表(筛选项)")
    public R<List<Common>> getRolePartList(@Schema(description = "名称") @RequestParam(required = false) String name) {
        return R.ok(adminRoleServiceImpl.getRolePartList(name));
    }

}
